package com.itheima.atm;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    private ArrayList<Account> accounts = new ArrayList<>();

    public void systemMenu() {
        while (true) {
            System.out.println("欢迎进入黑马银行ATM系统");
            System.out.println("1.用户登录");
            System.out.println("2.用户开户");
            System.out.println("请选择:");
            Scanner scanner = new Scanner(System.in);
            int a = scanner.nextInt();
            switch (a) {
                case (1):
                    System.out.println("============系统登录界面===============");
                    login();
                    break;
                case (2):
                    register();
                    break;
                default:
                    System.out.println("没有这个操作");
            }
        }
    }

    private void login() {
        //用户登录
        boolean flag = false;
        if (accounts.size() == 0) {
            System.out.println("当前系统无账户");
        } else {
            System.out.println("请输入您的卡号");
            Scanner scanner = new Scanner(System.in);
            int a = scanner.nextInt();
            boolean b = identifyCount(a);
            if (b) {
                System.out.println("请输入您的密码");
                int password = scanner.nextInt();
                Account account = loginIn(a, password);
                if (account != null) {
                    usersystem(account);
                } else {
                }
            } else {

            }
        }
    }

    public Account loginIn(int number, int password) {
        //登录账号密码
        Account ac = new Account();
        boolean a = false;
        for (int i = 0; i < accounts.size(); i++) {
            ac = accounts.get(i);
            if (ac.getCardNumber() == number && ac.getPassword() == password) {
                a = true;
                System.out.println("账号密码正确，恭喜你成功进入系统");
                return ac;
            }
        }
        if (a == false) {
            System.out.println("密码不正确");
        }
        return null;
    }


    private Account register() {
        //用户开户
        //1.先创建一个账户对象
        Account account = new Account();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入账户用户名");
        String name = scanner.next();
        account.setName(name);
        while (true) {
            System.out.println("请输入您的性别");
            String c = scanner.next();
            boolean b1 = identifySex(c);
            if (b1) {
                account.setSex(c);
                break;
            }
        }
        System.out.println("请输入输入账户密码：");
        int password = scanner.nextInt();
        System.out.println("请再次确认密码");
        while (true) {
            int password2 = scanner.nextInt();
            boolean b2 = identifyPassword(password, password2);
            if (b2) {
                account.setPassword(password2);
                break;
            } else {
                System.out.println("两次输入不一致,请重新输入");
            }
        }
        System.out.println("请输入账户每次提取限额");
        double a = scanner.nextDouble();
        account.setLimit(a);
        String m = modifySex(account.getSex());
        int number = randomCardId();
        account.setCardNumber(number);
        System.out.println("恭喜你，黑马" + name + m + "成功开户成功，您的卡号是:" + number);
        this.accounts.add(account);
        return account;

    }

    private boolean identifyCount(int cardnumber) {
        //验证用户,找到了账号放回true，没找到返回false
        boolean a = false;
        for (int i = 0; i < accounts.size(); i++) {
            Account ac = accounts.get(i);
            if (ac.getCardNumber() == cardnumber) {
                a = true;
            }
        }
        if (a == false) {
            System.out.println("请输入正确的卡号");
        }
        return a;
    }


    private boolean identifySex(String sex) {
        //判断性别是否有误
        boolean a = true;
        if (sex.equals("男") || sex.equals("女")) {
        } else {
            a = false;
            System.out.println("您输入的性别有错误");
        }
        return a;
    }

    private boolean identifyPassword(int password1, int password2) {
        //验证两次密码是否一致
        boolean b = true;
        if (password1 == password2) {
        } else {
            b = false;
        }
        return b;
    }

    private String modifySex(String sex) {
        String m;
        if (sex.equals("男")) {
            m = "先生";

        } else {
            m = "女士";
        }
        return m;
    }

    private int randomCardId() {
        //生成随机卡号
        StringBuilder cardNumber = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            int n = random.nextInt(10);
            cardNumber.append(n);
        }
        int card = Integer.parseInt(cardNumber.toString());
        return card;
    }

    //用户界面
    private void usersystem(Account account) {
        //登录成功的界面
        boolean flag = true;
        while (flag) {
            System.out.println("=======" + account.getName() + "，您可以办理以下业务" + "==========");
            System.out.println("1.查询账户");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            System.out.println("7.注销账户");
            Scanner scanner = new Scanner(System.in);
            int a = scanner.nextInt();
            switch (a) {
                case (1):
                    searchAccount(account);
                    break;
                case (2):
                    deposit(account);
                    break;
                case (3):
                    drawMoney(account);
                    break;
                case (4):
                    switchMoney(account);
                    break;
                case (5):
                    modifyPassword(account);
                    break;
                case (6):
                    systemMenu();
                    break;
                case (7):
                    break;
            }
        }

    }

    //1.用户界面-查询用户
    private void searchAccount(Account account) {
        System.out.println("===============账户信息================");
        System.out.println("卡号：" + account.getCardNumber());
        System.out.println("户主：" + modifySex(account.getSex()));
        System.out.println("性别：" + account.getSex());
        System.out.println("余额：" + account.getMoney());
        System.out.println("限额：" + account.getLimit());
    }

    //2.用户界面-存款
    private void deposit(Account account) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您的存款：");
        double d = scanner.nextDouble();
        account.setMoney(account.getMoney() + d);
    }

    //3.用户界面-用户取款
    private void drawMoney(Account account) {
        boolean flag = true;
        while (flag) {
            System.out.println("=======用户取款操作========");
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入您的取款金额：");
            double d = scanner.nextDouble();
            boolean b = compareMoney(d, account);
            if (b == false) {
                System.out.print("取款失败");
                System.out.println(" ");
            } else {
                account.setMoney(account.getMoney() - d);
                System.out.println("成功取出：" + d);
                flag = false;
            }
        }
    }

    //4.用户界面-转账
    private void switchMoney(Account account) {
        if (accounts.size() < 2) {
            System.out.println("当前系统中,不足2个账户,不能进行转账，请去开户吧");
            return;
        }
        System.out.println("==============转账系统=============");
        if (account.getMoney() == 0) {
            System.out.println("账户余额为0,请先存款");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        boolean flag1 = true;
        int m = 0;
        while (flag1) {
            System.out.println("请您输入对方的卡号:");
            int c = scanner.nextInt();
            flag1 = !identifyCount(c);
            m = c;
        }

        System.out.println("请输入您的转账金额：");
        double d = scanner.nextDouble();
        boolean b = compareMoney(d, account);
        if (b == false) {
            System.out.print("转账失败");
            System.out.println(" ");
        } else {
            Account account1 = findAccont(m);
            account1.setMoney(account1.getMoney() + d);
            account.setMoney(account.getMoney() - d);
            System.out.println("成功转出：" + d);
        }
    }

    //5.用户界面-修改账号密码
    private void modifyPassword(Account account) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您的密码");
        int a = scanner.nextInt();
        boolean b = identifyPassword(a, account.getPassword());
        if (b) {
            System.out.println("输入您要修改的密码");
            int c = scanner.nextInt();
            System.out.println("再次输入您要修改的密码");
            int d = scanner.nextInt();
            boolean b1 = identifyPassword(c, d);
            if (b1) {
                account.setPassword(d);
                System.out.println("修改密码成功");
            } else {
                System.out.println("密码修改失败");
            }
        } else {
            System.out.println("密码输入失败");
        }

    }

    //账户余额不足的提示
    private boolean compareMoney(double a, Account account) {
        boolean flag = false;
        if (a > account.getLimit()) {
            System.out.print("您的账户限额为" + account.getLimit() + ",");
            return false;
        } else {
            if (a > account.getMoney()) {
                System.out.println("余额不足");
                flag = false;//转账失败
            } else {
                flag = true;//转账成功
            }
        }
        return flag;
    }


    //根据id返回账户
    private Account findAccont(int cardNumber) {
        for (int i = 0; i < accounts.size(); i++) {
            Account ac = accounts.get(i);
            if (cardNumber == ac.getCardNumber()) {
                return ac;
            }
        }
        return null;
    }
}

